package com.kchouse.homepage;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.transition.Visibility;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.gxz.PagerSlidingTabStrip;
import com.kchouse.bean.MerchantItem;
import com.kchouse.bean.ProjectType;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.ListFragment;
import com.kchouse.R;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class MerchantsActivity extends BaseActivity implements View.OnClickListener {

    private MerchantsActivity mContent;
    private ArrayList<Integer> listhomebanner = new ArrayList<>();
    private ScrollView sv_item;
    private TextView tv_title;
    private ImageView iv_back;
    private RadioGroup rg_item;
    private RadioButton rb_item1;
    private RadioButton rb_item2;
    private PagerSlidingTabStrip pst;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private ArrayList<ProjectType> listproject = new ArrayList<>();//项目分类
    private PagerSlidingTabStrip pst2;
    private ViewPager viewPager2;
    private ArrayList<Fragment> fragments2 = new ArrayList<>();
    private ArrayList<ProjectType> listproject2 = new ArrayList<>();//项目分类
    //声明pst的标题
    String[] titles = {"洗车", "保养", "室内清洗", "喷漆", "油路清洗", "玻璃贴膜", "电子产品"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchants);



        mContent = this;
        addAct(mContent);
        intiData();
        intiView();
    }

    private void intiView() {

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("商家优惠");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);


        intiRG();

//        intiPSTS();

    }


    /**
     * 初始化单品tab
     */
    private void intiPSTS() {

        pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.pager);

        fragments.clear();
        for (int i = 0; i < listproject.size(); i++) {
            ProjectType item = listproject.get(i);
//            fragments.add(new ListFragment(10,"商家优惠"));
            fragments.add(new ListProjcetFragment(item.getId(), item.getName(), 1));
        }
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pst.setViewPager(viewPager);
        viewPager.setCurrentItem(0);

// 设置Tab的分割线的颜色
        pst.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst.setTextSize(13);
// 设置选中Tab文字的颜色
        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst.setTabPaddingLeftRight(12);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst.setSmoothScrollWhenClickTab(true);
    }

    /**
     * 初始化套餐tab2
     */
    private void intiPSTS2() {

        pst2 = (PagerSlidingTabStrip) findViewById(R.id.tabs2);
        viewPager2 = (ViewPager) findViewById(R.id.pager2);

        fragments2.clear();
        for (int i = 0; i < listproject2.size(); i++) {
            ProjectType item = listproject2.get(i);
//            fragments2.add(new ListFragment(10,"订单待评价"));
            fragments2.add(new ListProjcetFragment(item.getId(), item.getName(), 0));
        }
        viewPager2.setAdapter(new MyPagerAdapter2(getSupportFragmentManager()));
        pst2.setViewPager(viewPager2);
        viewPager2.setCurrentItem(0);

// 设置Tab的分割线的颜色
        pst2.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst2.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst2.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst2.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst2.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst2.setTextSize(13);
// 设置选中Tab文字的颜色
        pst2.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst2.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst2.setTabPaddingLeftRight(12);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst2.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst2.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst2.setSmoothScrollWhenClickTab(true);
    }


    /**
     * * 自定义适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return listproject.get(position).getName();
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    /**
     * * 自定义适配器2
     */
    class MyPagerAdapter2 extends FragmentPagerAdapter {

        public MyPagerAdapter2(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return listproject2.get(position).getName();
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments2.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments2.size();
        }
    }

    /**
     * 初始化单选框
     */
    private void intiRG() {
        rg_item = (RadioGroup) findViewById(R.id.rg_item);
        rb_item1 = (RadioButton) findViewById(R.id.rb_item1);
        rb_item2 = (RadioButton) findViewById(R.id.rb_item2);
        rb_item2.setChecked(true);
        rg_item.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_item1) {
//                    ToastUtils.getInstanc(mContent).showToast("单品");
//                    viewPager.setCurrentItem(0);

                    pst.setVisibility(View.VISIBLE);
                    viewPager.setVisibility(View.VISIBLE);
                    pst2.setVisibility(View.GONE);
                    viewPager2.setVisibility(View.GONE);
                    viewPager.setCurrentItem(viewPager2.getCurrentItem());

                } else {
//                    ToastUtils.getInstanc(mContent).showToast("套餐");
//                    viewPager2.setCurrentItem(0);
                    pst.setVisibility(View.GONE);
                    viewPager.setVisibility(View.GONE);
                    pst2.setVisibility(View.VISIBLE);
                    viewPager2.setVisibility(View.VISIBLE);
                    viewPager2.setCurrentItem(viewPager.getCurrentItem());

                }
            }
        });
    }

    private void intiData() {

        GetprojectType();
    }

    /**
     * 获取商家项目类型
     */
    private void GetprojectType() {

        String url = API.GetprojectType;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
//        params.addFormDataPart("name", name);
//        params.addFormDataPart("page", page);
//        params.addFormDataPart("pageSize", 10);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    listproject.addAll(JSONObject.parseArray(datastr, ProjectType.class));
                    listproject2.addAll(JSONObject.parseArray(datastr, ProjectType.class));

                    intiPSTS();
                    intiPSTS2();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                delAct(mContent);
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            delAct(mContent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
